package graphique;

import java.awt.event.ActionEvent;
import java.util.Map;
import java.util.Set;

import javax.swing.DefaultListModel;
import javax.swing.JList;

import modele.Contact;

public class ListContacts extends JList {

	private static final long serialVersionUID = 1L;
	private Map<String, Contact> contacts;
	private DefaultListModel model;

	
	public ListContacts(Map<String, Contact> contacts) {
	
		super();
		this.contacts = contacts;
		creerList();
		setVisible(true);
	}
	
	
	private void creerList() {
		
		Set<String> keys = contacts.keySet();
		model = new DefaultListModel();
		String value = "ID     | " + "Nom            " + "Prenom";
		model.addElement(value);
		
		for(String key: keys) {
			Contact tmp = contacts.get(key);
			value = key + "      | " + tmp.getNom() + "          " + tmp.getPrenom();
			model.addElement(value);
		}
		
		setModel(model);
	}
	
	
	public int getNbContact() {
		return model.getSize();
	}
	
	
	public String get(int index) {
		return (String)model.get(index);
	}
	
	
	public Contact getContact(String key) {
		
		if(!key.contains("I")) {
			return contacts.get(key);
		} else {
			return new Contact();
		}
	}

}
